<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.getList()}"
            @reset="()=>{this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="任务名称" name="jobName">
            <t-input
              v-model="queryParams.jobName"
              clearable
              placeholder="请输入任务名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="执行状态" name="isSuccess">
            <t-select style="width: 240px" placeholder="执行状态"
                      v-model="queryParams.isSuccess">
              <t-option value="1" label="成功"></t-option>
              <t-option value="0" label="失败"></t-option>
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      :selected-row-keys="selectedRowKeys"
      @select-change="(val)=>{selectedRowKeys=val}"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #isSuccess="{row}">
        <t-tag v-if="row.isSuccess === '1'" theme="success" variant="light">成功</t-tag>
        <t-tag v-else theme="danger" variant="light">失败</t-tag>
      </template>
      <template #op="{ row }">

        <t-button @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-table>
  </div>
</template>

<script>

export default {
  name: "job-log",
  data() {
    return {
      selectedRowKeys: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: '',
        isSuccess: '',
        jobId: null
      },
      data: [],
      columns: [
        {
          colKey: "jobName",
          title: "任务名称",
          align: "center",
        },
        {
          colKey: "beanName",
          title: "Bean名称",
          align: "center"
        },
        {
          colKey: "methodName",
          title: "方法名称",
          align: "center",
        },
        {
          colKey: "cronExpression",
          title: "cron表达式",
          align: "center"
        },
        {
          colKey: "exceptionDetail",
          title: "异常信息",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "isSuccess",
          title: "执行状态",
          align: "center"
        },
        {
          colKey: "startTime",
          title: "执行时间",
          align: "center",
          width: 180
        },
        {
          colKey: "endTime",
          title: "结束时间",
          align: "center",
          width: 180
        },
        {
          colKey: "time",
          title: "耗时(毫秒)",
          align: "center"
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 100
        },
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      dialog: {
        open: false,
        header: '',
        objId: null
      }
    }
  },
  created() {
    const jobId = this.$route.params && this.$route.params.jobId;
    if (jobId && jobId !== '0') {
      this.queryParams.jobId = jobId
    }
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.system.monitor.jobLogList(this.queryParams).then(response => {
        this.data = response.rows;
        this.pagination.total = response.total;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    delObj(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除调度日志编号为"${row.id}"的数据项？`,
        theme: "warning",
        onConfirm: ({e}) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.system.monitor.jobLogDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
